package jmine.tec.rpc.common.request;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * Bean contendo resultado da execução de uma requisição REST.
 * 
 * @author lundberg
 */
public class RestResponse {

    private Object result;

    private Throwable exception;

    /**
     * Construtor
     * 
     * @param result result
     */
    public RestResponse(Object result) {
        this.result = result;
    }

    /**
     * Construtor
     * 
     * @param exception exception
     */
    public RestResponse(Throwable exception) {
        this.exception = exception;
    }

    /**
     * Verifica se a requisição resultou em exceção
     * 
     * @return boolean
     */
    public boolean exceptionThrown() {
        return this.exception != null;
    }

    /**
     * Construtor
     */
    public RestResponse() {
    }

    /**
     * @return the result
     */
    public Object getResult() {
        return this.result;
    }

    /**
     * @param result the result to set
     */
    public void setResult(Object result) {
        this.result = result;
    }

    /**
     * @return the exception
     */
    public Throwable getException() {
        return this.exception;
    }

    /**
     * @param exception the exception to set
     */
    public void setException(Throwable exception) {
        this.exception = exception;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof RestResponse)) {
            return false;
        }
        RestResponse o = (RestResponse) obj;
        return new EqualsBuilder().append(this.result, o.result).append(this.exception, o.exception).isEquals();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.result).append(this.exception).toHashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
